$(function() {
    /**
    * 支付
    */
    (function(){
        disabled = false;
        $('#btn-pay').on('click', function() {
        	if(disabled){
        		alert('数据正在提交中');
        		return false;
        	}
        	disabled = true;
        	$.ajax({
    	        url:url,
    	        type:"post",
    	        data:"pay_type="+pay_type+"&id="+id+"&open_id="+open_id,
    	        beforeSend:function(){
    	        },
    	        success:function(data){
    	        	if (0 == data.status) {
    	        		alert(data.info)
    	            }else{
    	            	switch(data.data.pay_type){
		            		case 'wechatpay':
		            			break;
		            		case 'alipay':
		            			window.location.href = data.data.url;
		            			break;
		            		case 'wechatwappay':
		            			window.api_code = eval("(" + data.data.api_code + ")");
		    	            	callpay();
		            			break;
		            	}
    	            }
    	        	disabled = false;
    	        },
    	        error:function (XMLHttpRequest, textStatus, errorThrown) {
    	          alert("请求错误："+textStatus)
    	          disabled = false;
    	        }
    	    });
        	
        });
    })();
});


//调用微信JS api 支付
function jsApiCall(){
  WeixinJSBridge.invoke(
      'getBrandWCPayRequest',
      window.api_code,
      function(res){
    	  document.writ(res.err_msg);
         // WeixinJSBridge.log(res.err_msg);
          //alert(res.err_code+res.err_desc+res.err_msg);
      }
  );
}

function callpay(){
  if (typeof WeixinJSBridge == "undefined"){
      if( document.addEventListener ){
          document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
      }else if (document.attachEvent){
          document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
          document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
      }
  }else{
      jsApiCall();
  }
}

